package com.itggm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itggm.DTO.RecruitPageDTO;
import com.itggm.mapper.RecruitMapper;
import com.itggm.pojo.Recruit;
import com.itggm.response.ResponseResult;
import com.itggm.service.RecruitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: ggm
 * @data 2024年11月21日 11:52
 * 招聘管理
 */
@Service
public class RecruitServiceImpl implements RecruitService {
    @Autowired
    private RecruitMapper recruitMapper;


    @Override
    public ResponseResult page(RecruitPageDTO recruitPageDTO) {
        PageHelper.startPage(recruitPageDTO.getPage(), recruitPageDTO.getLimit());
        List<Recruit> recruitList = recruitMapper.page(recruitPageDTO);
        PageInfo<Recruit> recruitPageInfo = new PageInfo<>(recruitList);
        long total = recruitPageInfo.getTotal();
        List<Recruit> list = recruitPageInfo.getList();
        return ResponseResult.success().count(total).data(list);
    }

    @Override
    public ResponseResult add(Recruit recruit) {
        recruitMapper.add(recruit);
        return ResponseResult.success().message("添加成功");
    }

    @Override
    public ResponseResult delete(List<Integer> ids) {
        for (Integer id : ids) {
           //如果状态为1，则删除，否则不能删除
            Recruit recruit = recruitMapper.getById(id);
            if(recruit.getIsDelete()!=0){
                recruitMapper.delete(id);
            }else {
                return ResponseResult.fail().message("请先禁用再删除");
            }
        }
        return ResponseResult.success().message("删除成功");
    }

    @Override
    public ResponseResult edit(Recruit recruit) {
        recruitMapper.edit(recruit);
        return ResponseResult.success().message("修改成功");
    }

    @Override
    public ResponseResult editStatus(Integer id, Integer isDelete, Integer version) {
        recruitMapper.editStatus(id,isDelete,version);
        return ResponseResult.success().message("状态修改成功");
    }

    @Override
    public ResponseResult RecruitList(Integer limit) {
        List<Recruit> recruitList = recruitMapper.RecruitList(limit);
        return ResponseResult.success().data(recruitList);
    }
}
